package com.ohxwj.modules.sys.service.impl;

import com.ohxwj.modules.sys.dao.SysSchoolDao;
import com.ohxwj.modules.sys.entity.SysSchoolEntity;
import com.ohxwj.modules.sys.service.SysSchoolService;
import com.qiniu.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service("sysSchoolService")
public class SysSchoolServiceImpl implements SysSchoolService {
	@Autowired
	private SysSchoolDao sysSchoolDao;
	
	@Override
	public SysSchoolEntity queryObject(Long schoolId){
		return sysSchoolDao.queryObject(schoolId);
	}
	
	@Override
	public List<SysSchoolEntity> queryList(Map<String, Object> map){
		return sysSchoolDao.queryList(map);
	}
	
	@Override
	public void save(SysSchoolEntity sysDept){
		sysSchoolDao.save(sysDept);
	}
	
	@Override
	public void update(SysSchoolEntity sysDept){
		sysSchoolDao.update(sysDept);
	}
	
	@Override
	public void delete(Long schoolId){
		sysSchoolDao.delete(schoolId);
	}

	@Override
	public List<Long> querySchoolIdList(Long parentId) {
		return sysSchoolDao.querySchoolIdList(parentId);
	}

	public List<HashMap> checkUser(Map<String, Object> map){
		return sysSchoolDao.checkUser(map);
	}

	@Override
	public String getSubSchoolIdList(Long schoolId){
		//部门及子校区ID列表
		List<Long> schoolIdList = new ArrayList<>();

		//获取子校区ID
		List<Long> subIdList = querySchoolIdList(schoolId);
		getSchoolTreeList(subIdList, schoolIdList);

		//添加本校区
		schoolIdList.add(schoolId);

		String schoolFilter = StringUtils.join(schoolIdList, ",");
		return schoolFilter;
	}

	/**
	 * 递归
	 */
	public void getSchoolTreeList(List<Long> subIdList, List<Long> schoolIdList){
		for(Long schoolId : subIdList){
			List<Long> list = querySchoolIdList(schoolId);
			if(list.size() > 0){
				getSchoolTreeList(list, schoolIdList);
			}

			schoolIdList.add(schoolId);
		}
	}


	@Override
	public SysSchoolEntity getSchoolInfo(Map<String, Object> map){
		return sysSchoolDao.getSchoolInfo(map);
	}



}
